package com.code.leetcode.offer;

import com.code.leetcode.utils.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/8/1 11:27
 */
public class The32_3 {

    List<List<Integer>> lists = new ArrayList<>();

    public List<List<Integer>> levelOrder(TreeNode root) {
        dfs(root, 1);
        return lists;
    }
    public void dfs(TreeNode node , int index) {
        if (node == null) return;
        if (index > lists.size()) {
            lists.add(new ArrayList<>());
        }
        if ((index & 1) == 1) {
            lists.get(index - 1).add(node.val);
        } else {
            lists.get(index - 1).add(0,node.val);
        }
        dfs(node.left, index + 1);
        dfs(node.right, index + 1);
    }
}
